<template>
  <div class="dashboard-container">
    <div class="container">
      <div class="tableBar">
        <label style="margin-right: 5px">员工姓名：</label>
        <el-input
          v-model="input"
          placeholder="请输入员工姓名"
          style="width: 15%"
          clearable
          @clear="init"
          @keyup.enter.native="initFun"
        />
        <el-button class="normal-btn continue" @click="init(true)">
          查询
        </el-button>
        <el-button
          type="primary"
          style="float: right"
          @click="addEmployeeHandle('add', '')"
        >
          + 添加员工
        </el-button>
      </div>
      <el-table
        v-if="tableData.length"
        :data="tableData"
        stripe
        class="tableBox"
      >
        <el-table-column prop="name" label="员工姓名" />
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column label="账号状态">
          <template #default="scope">
            <div
              class="tableColumn-status"
              :class="{ 'stop-use': String(scope.row.status) === '0' }"
            >
              {{ String(scope.row.status) === '0' ? '禁用' : '启用' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间" />
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              :class="{ 'disabled-text': scope.row.username === 'admin' }"
              :disabled="scope.row.username === 'admin'"
              @click="addEmployeeHandle(scope.row.id, scope.row.username)"
            >
              修改
            </el-button>
            <el-button
              :disabled="scope.row.username === 'admin'"
              type="text"
              size="small"
              class="non"
              :class="{
                'disabled-text': scope.row.username === 'admin',
                blueBug: scope.row.status == '0',
                delBut: scope.row.status != '0'
              }"
              @click="statusHandle(scope.row)"
            >
              {{ scope.row.status == '1' ? '禁用' : '启用' }}
            </el-button>
            <!-- 新增删除按钮 -->
            <el-button
              :disabled="scope.row.username === 'admin'"
              type="text"
              size="small"
              class="delBut"
              @click="deleteEmployeeHandle(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Empty v-else :is-search="isSearch" />
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import HeadLable from '@/components/HeadLable/index.vue';
import { getEmployeeList, enableOrDisableEmployee, deleteEmployee } from '@/api/employee';
import { UserModule } from '@/store/modules/user';
import InputAutoComplete from '@/components/InputAutoComplete/index.vue';
import Empty from '@/components/Empty/index.vue';

@Component({
  name: 'Employee',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty,
  },
})
export default class extends Vue {
  private input: any = ''
  private counts: number = 0
  private page: number = 1
  private pageSize: number = 10
  private tableData = []
  private id = ''
  private status = ''
  private isSearch: boolean = false
  created() {
    this.init()
  }

  initProp(val) {
    this.input = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }

  get userName() {
    return UserModule.username
  }

  private async init(isSearch?: boolean) {
  this.isSearch = isSearch !== undefined ? isSearch : false; // 使用三元运算符确保 isSearch 有明确的布尔值
  const params = {
    page: this.page,
    pageSize: this.pageSize,
    name: this.input ? this.input : undefined,
  };
  await getEmployeeList(params)
    .then((res: any) => {
      if (String(res.data.code) === '1') {
        this.tableData = res.data && res.data.data && res.data.data.records;
        this.counts = res.data.data.total;
      }
    })
    .catch((err) => {
      this.$message.error('请求出错了：' + err.message);
    });
}

  // 添加
  private addEmployeeHandle(st: string, username: string) {
  if (st === 'add') {
    // 如果是添加新员工，只有管理员可以进行此操作
    if (this.userName !== 'admin') {
      this.$message.warning('只有管理员可以添加新员工');
      return;
    }
    this.$router.push({ path: '/employee/add' });
  } else {
    // 如果是修改员工信息，只有管理员或员工本人可以进行此操作
    if (this.userName !== 'admin' && this.userName !== username) {
      this.$message.warning('您只能修改自己的信息');
      return;
    }
    if (username === 'admin') {
      return;
    }
    this.$router.push({ path: '/employee/add', query: { id: st } });
  }
}

 // 状态修改
private statusHandle(row: any) {
  if (this.userName !== 'admin') {
    this.$message.warning('只有管理员可以修改账号状态');
    return;
  }
  if (row.username === 'admin') {
    return;
  }
  this.id = row.id;
  this.status = row.status;
  this.$confirm('确认调整该账号的状态?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    enableOrDisableEmployee({ id: this.id, status: !this.status ? 1 : 0 })
      .then((res) => {
        if (String(res.status) === '200') {
          this.$message.success('账号状态更改成功！');
          this.init();
        }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message);
      });
  });
}

// 删除员工
private deleteEmployeeHandle(row: any) {
  if (this.userName !== 'admin') {
    this.$message.warning('只有管理员可以删除员工');
    return;
  }
  if (row.username === 'admin') {
    return;
  }
  this.$confirm('确认删除该员工?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteEmployee({ id: row.id })
      .then((res) => {
        if (String(res.status) === '200') {
          this.$message.success('员工删除成功！');
          this.init();
        }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message);
      });
  });
}

  private handleSizeChange(val: any) {
    this.pageSize = val
    this.init()
  }

  private handleCurrentChange(val: any) {
    this.page = val
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.disabled-text {
  color: #bac0cd !important;
}
</style>
